package club.baibin0558.jstudy.file.service.impl;

import club.baibin0558.jstudy.core.vo.FileDetailsInfo;
import club.baibin0558.jstudy.file.service.FileService;
import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.FileInfo;
import com.github.tobato.fastdfs.domain.MataData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * fastdfs的服务上传实现
 */
@Service
public class FastDFSFileServiceImpl implements FileService {
    private static final Logger LOGGER = LoggerFactory.getLogger(FastDFSFileServiceImpl.class);
    private static final String[] SUPPORT_IMAGE_TYPE = { "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP" };
    private static final List<String> SUPPORT_IMAGE_LIST = Arrays.asList(SUPPORT_IMAGE_TYPE);
    @Value("${file.server.path}")
    private String fileServerPath;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private ThumbImageConfig thumbImageConfig;


    private Set<MataData> createMataData() {
        Set<MataData> metaDataSet = new HashSet<MataData>();
        metaDataSet.add(new MataData("Author", "白彬"));
        metaDataSet.add(new MataData("CreateDate", "2016-01-05"));
        return metaDataSet;
    }

    /**
     * 文件上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public FileDetailsInfo upload(MultipartFile multipartFile) {
        Set<MataData> metaDataSet = createMataData();
        StorePath storePath = null;
        String extName = fileExtName(multipartFile.getOriginalFilename());
        try {
            if(isSupportImage(extName)) {
                storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(),
                        multipartFile.getSize(), extName, metaDataSet);
            }else {
                storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(),
                        multipartFile.getSize(), extName, metaDataSet);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileDetailsInfo result = new FileDetailsInfo();
        result.setPath(fileServerPath + storePath.getFullPath());
        result.setFileName(multipartFile.getOriginalFilename());
        result.setSize(multipartFile.getSize());
        try {
            String slavePath = thumbImageConfig.getThumbImagePath(storePath.getPath());
            result.setSmallPath(fileServerPath + "group1/" + slavePath);
        } catch (Exception e) {
        }
        return result;
    }

    private String fileExtName(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 是否是支持的图片文件
     *
     * @param fileExtName
     * @return
     */
    private boolean isSupportImage(String fileExtName) {
        return SUPPORT_IMAGE_LIST.contains(fileExtName.toUpperCase());
    }
}
